<?php

namespace Sm\AssBundle\Parser;

use Sm\AssBundle\Entity\Ass as Ass;
use Sm\AssBundle\Entity\Style as Style;
use Sm\AssBundle\Entity\Srt as Srt;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Datetime;

class Parser {

    protected $state;
    protected $em;
    private $container;
    private $cpt_line;
    private $ass;

    public function __construct(ContainerInterface $container, EntityManager $entityManager) {
        $this->container = $container;
        $this->state = false;
        $this->cpt_line = 0;
        $this->em = $entityManager;
        $this->ass = null;
    }

    /*
     * Init parse
     */

    public function initParse(Ass $ass) {

        $root_dir =  $this->container->get('kernel')->getRootDir();
        
        $myFile = $root_dir.'/../web/files/ass/' . $ass->getAssname();
        
        //on sauvegarde l'ass
        $this->ass = $ass;
        
        try {
            $fh = fopen($myFile, "r");
            if ($fh) {
                while (!feof($fh)) {
                    $line = fgets($fh);

                    $this->oneLineParse($line);
                }
                fclose($fh);
            }
            
            $this->setState(1);
            
        } catch(Exception $e){
            
        }
        
    }
    
    /*
     * Parse one line
     */
    private function oneLineParse($line) {
        
        if(preg_match('/^Style: [A-Za-z]*/', $line)) {
            $this->styleParse($line);
        }
        
        if(preg_match('/^[A-Za-z]*: [0-9]{1},[0-9]{1}:[0-9]{2}:[0-9]{2}\.[0-9]{2},[0-9]{1}:[0-9]{2}:[0-9]{2}\.[0-9]{2},[A-Za-z]*,,[0-9]{4},[0-9]{4},[0-9]{4},,/', $line)) {
            $this->dialogueParse($line);
        }
        
        if(preg_match('/^PlayRes[X-Y]: [0-9]*/', $line)) {
            $this->resolutionParse($line);
        }
         
        $this->cpt_line++; 
    }
    
    /*
     * Parse un style
     */
    private function styleParse($line) {
        
        $line_replace = str_replace('Style: ', '', $line);
        $styles = explode(',', $line_replace);
                
        $options = array(
            'name' => $styles[0],
            'fontname' => $styles[1],
            'fontsize' => $styles[2],
            'primarycolour' => $styles[3],
            'secondarycolour' => $styles[4],
            'outlinecolour' => $styles[5],
            'backcolour' => $styles[6],
            'bold' => $styles[7],
            'italic' => $styles[8],
            'underline' => $styles[9],
            'strikeout' => $styles[10],
            'scalex' => $styles[11],
            'scaley' => $styles[12],
            'borderstyle' => $styles[15],
            'outline' => $styles[16],
            'shadow' => $styles[17],
            'alignment' => $styles[18],
            'marginl' => $styles[19],
            'marginr' => $styles[20],
            'marginv' => $styles[21],
            'encoding' => $styles[22],
            'ass' => $this->ass
        );
        
        $style = new Style($options);
        
        $this->em->persist($style);
        $this->em->flush();
        
    }
    
    /*
     * Parse un dialogue
     */
    private function dialogueParse($line) {
        
        $type = 'Dialogue';
        
        //On récupère le type de dialogue
        preg_match('/^[a-zA-Z]*/', trim($line), $type);
        
        //On récupère le reste
        $split_text = str_replace($type[0].': ', '', $line);
        $srts = $styles = explode(',', $split_text, 10);
              
        $style_rep = $this->em->getRepository('SmAssBundle:Style');
        $style = $style_rep->findOneByName($srts[3]);
        
        $options = array(
            'line' => $this->ass->getLineNumber(),
            'type' => $type[0],
            'start' => $srts[1],
            'end' => $srts[2],
            'content' => $srts[9],
            'last_modified' => new Datetime(),
            'style' => $style,
            'is_modified' => 0,
            'ass' => $this->ass
        );
        
        $srt = new Srt($options);
        
        $this->em->persist($srt);
        
    }
    
    /*
     * Parse une resolution
     */
    private function resolutionParse($line) {
        
        $resolution = 0;
        
        preg_match('/[0-9]*$/', trim($line), $resolution);
        
        if(strstr('X', $line)) {
            $this->ass->setPlayresx($resolution[0]);
        }
        elseif(strstr('Y', $line))
        {
            $this->ass->setPlayresy($resolution[0]);
        }
                
        $this->em->persist($this->ass);
    }
    
    public function getState() {
        return $this->state;
    }

    public function setState($value) {
        $this->state = $value;
    }

}
